//
//  LXJEmoticonToolManager.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/4/7.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit

/// 表情工具管理类

/// 设置每页显示多少个表情
let emoticonNumberOfPage = 20

class LXJEmoticonToolManager: NSObject {
    /// 单例 全局访问点
    static let sharedManager: LXJEmoticonToolManager = {
        let manager = LXJEmoticonToolManager()
        return manager
    }()
    
    ///  创建emoticonsBundle对象
    lazy var emoticonBundle: Bundle = {
        ///  获取bundle的路径
        let bundlePath = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        ///  根据路径创建bundle对象
        let bundle = Bundle(path: bundlePath)!
        return bundle
    }()
    
    ///  默认表情
    fileprivate lazy var defualtEmoticonArray: [LXJEmoticon] = {
        return self.getEmoticonArray(folderName: "default", fileName: "info.plist")
    }()
    ///  emoji表情
    fileprivate lazy var emojiEmoticonArray: [LXJEmoticon] = {
        return self.getEmoticonArray(folderName: "emoji", fileName: "info.plist")
    }()
    ///  浪小花表情
    fileprivate lazy var lxhEmoticonArray: [LXJEmoticon] = {
        return self.getEmoticonArray(folderName: "lxh", fileName: "info.plist")
    }()
    /// 最近表情
    fileprivate lazy var recentEmoticonArray: [[LXJEmoticon]] = [[LXJEmoticon]]()
    
    /// 全部的表情包
    lazy var allEmoticonArray: [[[LXJEmoticon]]] = {
        return [
            /// 最近的表情包
            self.recentEmoticonArray,
            /// 默认表情
            self.numberOfSectionInEachSection(emoticonArray: self.defualtEmoticonArray),
            /// Emoji 表情包
            self.numberOfSectionInEachSection(emoticonArray: self.emojiEmoticonArray),
            /// 浪小花表情包
            self.numberOfSectionInEachSection(emoticonArray: self.lxhEmoticonArray)
        ]
    }()
    
    
    ///  根据传入的文件夹名和文件的名字获取对应的表情数据
    fileprivate func getEmoticonArray(folderName: String, fileName: String) -> [LXJEmoticon] {
        ///  获取表情数据的子路径
        let subPath = folderName + "/" + fileName
        let path = self.emoticonBundle.path(forResource: subPath, ofType: nil)!
        ///  读取info.plist文件数据
        let dicArray = NSArray(contentsOfFile: path) as! [[String: Any]]
        ///  使用YYModel完成数组字典转成数组模型
        let modelArray = NSArray.yy_modelArray(with: LXJEmoticon.self, json: dicArray) as! [LXJEmoticon]
        /// 遍历数组 拼接图片表情的全路径
        for (_, emoticonModel) in modelArray.enumerated() {
            /// 如果是图片表情
            if emoticonModel.type == "0" {
                /// 获取图片表情的全路径
                let path = emoticonBundle.path(forResource: folderName, ofType: nil)! + "/" + emoticonModel.png!
                /// 赋值给表情模型
                emoticonModel.path = path
            }
        }
        return modelArray
    }
    
    /// 计算每一种表情分成多少页进行显示 并完成一个二维数组的拼接
    fileprivate func numberOfSectionInEachSection(emoticonArray: [LXJEmoticon]) -> [[LXJEmoticon]] {
        ///根据传入的数组个数计算页数
        let pageNumber = (emoticonArray.count - 1) / emoticonNumberOfPage + 1
        /// 定义一个临时的二维数组
        var tempArr = [[LXJEmoticon]]()
        /// 遍历传入的表情数组
        for index in 0..<pageNumber {
            /// 获取开始截取的索引
            let loc = index * emoticonNumberOfPage
            /// 获取截取的长度
            var len = emoticonNumberOfPage
            
            /// 判断是否越界
            if (loc + len) > emoticonArray.count {
                /// 在超出部分后获取剩余个数
                len = emoticonArray.count - loc
            }
            /// 截取范围
            let range = NSMakeRange(loc, len)
            
            /// 根据截取范围截取数组
            let subArr = (emoticonArray as NSArray).subarray(with: range) as! [LXJEmoticon]
            
            /// 往临时数组追加元素
            tempArr.append(subArr)
        }
        return tempArr
    }
    
}
